package Boundary;
import Entity.Achievements;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;
import static Control.Control.*;

/**
 * @author Group 77
 * @version 4.0
 * @description display an operational page for achievement
 */
public class AchieveManagement extends Overall{
    public AchieveManagement(int number){
        super("Achievements Management");

        //go back to upper level
        panel1.setLayout(new BorderLayout());
        panel1.setBorder(BorderFactory.createEmptyBorder());
        JButton button0 = new JButton("   Go back   ");
        button0.setBorder(BorderFactory.createRaisedBevelBorder());
        button0.setBackground(Color.cyan);
        button0.setFont(new Font("STSong", Font.BOLD, 30));
        JLabel label1 = new JLabel("Achievements  ", JLabel.CENTER);
        label1.setFont(new Font("TimesRoman", Font.ITALIC, 30));
        JPanel empty1 = new JPanel();
        empty1.setPreferredSize(new Dimension(100, 10));
        panel1.add(button0, BorderLayout.WEST);
        panel1.add(empty1, BorderLayout.NORTH);
        panel1.add(label1, BorderLayout.EAST);
        Add_Component(jfr, gbl, panel1, gbc, 0, 0, 4, 1, 1, 0);


        button0.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new Start(number);
                        jfr.dispose();
                    }
                }
        );

        panel2.setLayout(new BorderLayout());
        panel2.setBorder(BorderFactory.createEmptyBorder());
        JLabel label2 = new JLabel("Achievements Management", JLabel.CENTER);
        label2.setFont(new Font("TimesRoman", Font.ITALIC, 30));
        JPanel empty2 = new JPanel();
        panel2.add(empty2, BorderLayout.NORTH);
        panel2.add(label2, BorderLayout.CENTER);
        Add_Component(jfr, gbl, panel2, gbc, 0, 0, 4, 1, 1, 0);

        panel3.setLayout(new BorderLayout());
        panel3.setBorder(BorderFactory.createEmptyBorder());
        Object[] columnNames = {"Name", "Achievements", "Experience"};
        List<Achievements> achievement=json2.readArray(Achievements.class);
        List list = new ArrayList();


        int i = 0;
        while(i < achievement.size()){
            if(number == achievement.get(i).getStudentNum()){
                String[] str = {achievement.get(i).getStudentName(),achievement.get(i).getAchievement(),achievement.get(i).getExperience()};
                list.add(str);
            }
            i++;
        }
        Object[] s = (Object[])list.toArray();
        String[][] rowData = new String[s.length][];
        for(int j=0;j<s.length;j++){
            rowData[j] = (String[])s[j];
        }

        JTable table1 = new JTable (rowData, columnNames);
        table1.setForeground(Color.BLACK);
        table1.setFont(new Font(null, Font.PLAIN, 14));
        table1.setSelectionForeground(Color.DARK_GRAY);
        table1.setSelectionBackground(Color.cyan);
        table1.setGridColor(Color.GRAY);
        table1.setBackground(Color.white);
        table1.getTableHeader().setFont(new Font(null, Font.BOLD, 14));
        table1.getTableHeader().setForeground(Color.BLACK);
        table1.getTableHeader().setBackground(new Color(113,191,234));
        table1.getTableHeader().setResizingAllowed(false);
        table1.getTableHeader().setReorderingAllowed(false);
        table1.setRowHeight(30);
        table1.getColumnModel().getColumn(0).setPreferredWidth(40);
        JScrollPane scrollPane = new JScrollPane(table1);
        table1.setPreferredScrollableViewportSize(new Dimension(400, 420));
        panel3.add(scrollPane);
        Add_Component(jfr,gbl,panel3,gbc,0,5,1,1,1,0);


        panel5.setLayout(new BorderLayout());
        panel5.setBorder(BorderFactory.createEmptyBorder());
        JButton button1 = new JButton(" Update ");
        JButton button2 = new JButton(" Delete  ");
        button2.setBorder(BorderFactory.createRaisedBevelBorder());
        button2.setBackground(Color.cyan);
        button2.setFont(new Font("STSong", Font.BOLD, 25));
        button1.setBorder(BorderFactory.createRaisedBevelBorder());
        button1.setBackground(Color.cyan);
        button1.setFont(new Font("STSong", Font.BOLD, 25));
        JPanel empty3 = new JPanel();
        empty1.setPreferredSize(new Dimension(100, 10));
        panel5.add(button1, BorderLayout.WEST);
        panel5.add(button2, BorderLayout.EAST);
        panel5.add(empty3, BorderLayout.NORTH);
        Add_Component(jfr, gbl, panel5, gbc, 0, 8, 1, 1, 1, 0);


        button1.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new AchievementAdd(number);
                        jfr.dispose();
                    }
                }
        );

        button2.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jfr.setVisible(false);
                        new AchievementDelete(number);
                        jfr.dispose();
                    }
                }
        );



    }



}
